﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySql.Web;
using System.Configuration;

namespace Artemis
{
    public partial class ItemQuery : Form
    {
        public string connectionstring = null;
        public Boolean switchdb = false;
        public static string ItemQueryDes(string typeID,string connectionstring)   //获取物品描述
        {
            DataSet itemdes = new DataSet();
            MySqlConnection iconn = null;
            iconn = new MySqlConnection();
            iconn.ConnectionString = connectionstring;
            string isql = null;
            isql = "SELECT description FROM eve_main.invtypes WHERE typeID = @typeID;";
            iconn.Open();
            MySqlCommand commd = new MySqlCommand("set names utf8", iconn);
            commd.ExecuteNonQuery();
            commd.Dispose();
            MySqlCommand query = new MySqlCommand(isql, iconn);
            query.Parameters.Add(new MySqlParameter("@typeID", typeID));
            MySqlDataAdapter mdad = new MySqlDataAdapter(query);
            
            mdad.Fill(itemdes, "ItemDes");
            if (itemdes != null && itemdes.Tables[0].Rows.Count != 0)
            {
                string des = null;
                des = itemdes.Tables["ItemDes"].Rows[0]["description"].ToString();
                return des;
            }
            else
            {
                string des = null;
                des = "该物品没有描述";
                return des;
            }
            

        }
        public static DataSet ItemAtt(string typeID,string connectionstring)              //获取物品特殊属性
        {
            DataSet Att = new DataSet();
            string asql = "SELECT typeID,attributeName,valueInt,valueFloat FROM dgmtypeattributes JOIN dgmattributetypes ON dgmtypeattributes.attributeID = dgmattributetypes.attributeID WHERE typeID = @typeID;";
            MySqlConnection iconn = null;
            iconn = new MySqlConnection();
            iconn.ConnectionString = connectionstring;
            iconn.Open();
            MySqlCommand commd = new MySqlCommand("set names utf8", iconn);
            commd.ExecuteNonQuery();
            commd.Dispose();
            MySqlCommand query = new MySqlCommand(asql, iconn);
            query.Parameters.Add(new MySqlParameter("@typeID", typeID));
            MySqlDataAdapter mdad = new MySqlDataAdapter(query);
            mdad.Fill(Att, "ItemAtt");
            return Att;

        }
        public static DataSet ItemInv(string typeID,string connectionstring) //获取物品普通信息
        {
            DataSet Att = new DataSet();
            string asql = "SELECT typeName,mass,volume,capacity FROM eve_main.invtypes WHERE typeID = @typeID;";
            MySqlConnection iconn = null;
            iconn = new MySqlConnection();
            iconn.ConnectionString = connectionstring;
            iconn.Open();
            MySqlCommand commd = new MySqlCommand("set names utf8", iconn);
            commd.ExecuteNonQuery();
            commd.Dispose();
            MySqlCommand query = new MySqlCommand(asql, iconn);
            query.Parameters.Add(new MySqlParameter("@typeID", typeID));
            MySqlDataAdapter mdad = new MySqlDataAdapter(query);
            mdad.Fill(Att, "ItemInv");
            return Att;
        }
        public static DataSet ItemCat(string typeID, string connectionstring)  //获取物品的分类
        {
            DataSet itemdes = new DataSet();
            MySqlConnection iconn = null;
            iconn = new MySqlConnection();
            iconn.ConnectionString = connectionstring;
            string isql = null;
            isql = "SELECT categoryID FROM eve_main.invgroups  WHERE groupID IN ( SELECT groupID FROM eve_main.invtypes WHERE typeID = @typeID);";
            iconn.Open();
            MySqlCommand commd = new MySqlCommand("set names utf8", iconn);
            commd.ExecuteNonQuery();
            commd.Dispose();
            MySqlCommand query = new MySqlCommand(isql, iconn);
            query.Parameters.Add(new MySqlParameter("@typeID", typeID));
            MySqlDataAdapter mdad = new MySqlDataAdapter(query);
            mdad.Fill(itemdes, "ItemCat");
            return itemdes;
        }
        public ItemQuery(Boolean Switch)
        {
            switchdb = Switch;
            if (Switch == true)
            {
                connectionstring = ConfigurationManager.ConnectionStrings["BackupDataBase"].ConnectionString.ToString();
            }
            else
            {
                connectionstring = ConfigurationManager.ConnectionStrings["MainDataBase"].ConnectionString.ToString();
            }
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string typeName = null;
            typeName = comboBox1.Text;
            string typeID = null;
            typeID = EveCentral.IDGET(typeName,connectionstring);
            string description = null;
            description = ItemQueryDes(typeID,connectionstring);
            string Cat = null;
            DataSet cat = new DataSet();
            cat = ItemCat(typeID,connectionstring);
            Cat = cat.Tables["ItemCat"].Rows[0]["categoryID"].ToString();
            if (description == null)
            {
                richTextBox1.Text = "该物品没有描述";
            }
            else
            {
                richTextBox1.Text = description;
            }
            if (typeID == "No such item")
            {
                string sql = null;
                sql = "SELECT typeName FROM `invtypes` WHERE typeName LIKE @typeName ORDER BY typeName DESC;";
                DataSet similarnames = new DataSet();
                MySqlCommand query = new MySqlCommand(sql);
                query.Parameters.Add(new MySqlParameter("@typeName", typeName + "%"));
                similarnames = EveCentral.getdata(query, "Names", connectionstring);
                if (similarnames != null && similarnames.Tables[0].Rows.Count != 0)
                {
                    List<object> names = new List<object>();
                    names = Agents.refreshnoemptyline(similarnames, "Names", "typeName");
                    comboBox1.DataSource = names;
                }
                else
                {
                    richTextBox1.Text = "该物品不存在";
                    highslotlbl.Text = "-";
                    medslotlbl.Text = "-";
                    lowslotlbl.Text = "-";
                    riglabel.Text = "-";
                    pglbl.Text = "-";
                    cpulbl.Text = "-";
                    equcpu.Text = "-";
                    equmeta.Text = "-";
                    equpg.Text = "-";
                    equtech.Text = "-";
                    maslbl.Text = "-";
                    vollbl.Text = "-";
                    comboBox1.DataSource = null;
                    comboBox1.Text = typeName;
                }
            }
            else
            {
                DataSet Att = new DataSet();
                Att = ItemAtt(typeID,connectionstring);
                DataSet inv = new DataSet();
                inv = ItemInv(typeID,connectionstring);
                if (inv != null && inv.Tables[0].Rows.Count != 0)
                {
                    string mass = null;
                    mass = inv.Tables["ItemInv"].Rows[0]["mass"].ToString();
                    maslbl.Text = mass + "KG";
                    string volume = null;
                    volume = inv.Tables["ItemInv"].Rows[0]["volume"].ToString();
                    vollbl.Text = volume + "m^3";
                }
                else
                {

                }
                if (description == "该物品没有描述")
                {
                    highslotlbl.Text = "-";
                    medslotlbl.Text = "-";
                    lowslotlbl.Text = "-";
                    riglabel.Text = "-";
                    pglbl.Text = "-";
                    cpulbl.Text = "-";
                }
                else
                {
                    
                    if (Cat == "7")  //物品分类为装备
                    {
                        if (Att != null && Att.Tables[0].Rows.Count != 0)
                        {

                        highslotlbl.Text = "-";
                        medslotlbl.Text = "-";
                        lowslotlbl.Text = "-";
                        riglabel.Text = "-";
                        pglbl.Text = "-";
                        cpulbl.Text = "-";
                        DataRow[] techlvl = Att.Tables["ItemAtt"].Select("attributename = 'techLevel'");
                        string hsct = techlvl[0].ItemArray[2].ToString();
                        equtech.Text = hsct;
                        DataRow[] ms = Att.Tables["ItemAtt"].Select("attributename = 'metaLevel'");
                        string msct = ms[0].ItemArray[2].ToString();
                        equmeta.Text = msct;
                        DataRow[] ls = Att.Tables["ItemAtt"].Select("attributename = 'cpu'");
                        string lsct = ls[0].ItemArray[2].ToString();
                        equcpu.Text = lsct + "TF";
                        DataRow[] rig = Att.Tables["ItemAtt"].Select("attributename = 'power'");
                        string rgct = rig[0].ItemArray[2].ToString();
                        equpg.Text = rgct + "MW";



                        }

                    }
                    if (Cat == "6")  //物品分类为舰船
                    {
                        if (Att != null && Att.Tables[0].Rows.Count != 0)
                        {
                            DataRow[] hghs = Att.Tables["ItemAtt"].Select("attributename = 'hiSlots'");
                            if (hghs.ToList<object>().Count != 0)
                            {
                                string hsct = hghs[0].ItemArray[3].ToString();
                                highslotlbl.Text = hsct + "个";
                            }
                            else
                            {
                                highslotlbl.Text = "-";
                            }
                            DataRow[] ms = Att.Tables["ItemAtt"].Select("attributename = 'medSlots'");
                            if (ms.ToList<object>().Count != 0)
                            {
                                string msct = ms[0].ItemArray[3].ToString();
                                medslotlbl.Text = msct + "个";
                            }
                            else
                            {
                                medslotlbl.Text = "-";
                            }
                            DataRow[] ls = Att.Tables["ItemAtt"].Select("attributename = 'lowSlots'");
                            if (ls.ToList<object>().Count != 0)
                            {
                                string lsct = ls[0].ItemArray[3].ToString();
                                lowslotlbl.Text = lsct + "个";
                            }
                            else
                            {
                                lowslotlbl.Text = "-";
                            }
                            DataRow[] rig = Att.Tables["ItemAtt"].Select("attributename = 'rigSlots'");
                            string rgct = rig[0].ItemArray[3].ToString();
                            if (rgct == "")
                            {
                                rgct = rig[0].ItemArray[2].ToString();
                            }
                            riglabel.Text = rgct + "个";
                            DataRow[] pg = Att.Tables["ItemAtt"].Select("attributename = 'powerOutput'");
                            string pgct = pg[0].ItemArray[3].ToString();
                            pglbl.Text = pgct + "MW";
                            DataRow[] cpu = Att.Tables["ItemAtt"].Select("attributename = 'cpuOutput'");
                            string cpuct = cpu[0].ItemArray[3].ToString();
                            cpulbl.Text = cpuct + "TF";
                        }
                        equcpu.Text = "-";
                        equmeta.Text = "-";
                        equpg.Text = "-";
                        equtech.Text = "-"; ;
                    }
                    else if(Cat!="6" & Cat!="7")
                    {
                        highslotlbl.Text = "-";
                        medslotlbl.Text = "-";
                        lowslotlbl.Text = "-";
                        riglabel.Text = "-";
                        pglbl.Text = "-";
                        cpulbl.Text = "-";
                        equcpu.Text = "-";
                        equmeta.Text = "-";
                        equpg.Text = "-";
                        equtech.Text = "-";
                  
                    }
                   
                }

            }
        }

        private void ItemQuery_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            EveCentral qu = new EveCentral(comboBox1.Text,switchdb);
            qu.Show();
            

        }

        private void equslot_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            button1_Click(sender, e);
        }
    }
}
